/**
 * 业务数据管理初始化
 */
var VpnBusinessDataInfo = {
    table1: null,
    table2: null,
    table3: null
};

/**
 * 初始化第一个表格列
 */
VpnBusinessDataInfo.initColumn1 = function(){
    return [[
        {field: 'createTime', title: '日期', fixed: 'left',align: 'center' }
        ,{field: 'todayDownloadCount', title: '当日下载人数',align: 'center' }
        ,{field: 'totalDownloadCount', title: '月累计下载人数',align: 'center' }
        ,{field: 'todayRegCount', title: '当日注册人数',align: 'center' }
        ,{field: 'totalRegCount', title: '累计注册人数',align: 'center' }
        ,{field: 'paymentCount', title: '当日注册及付费人数',width:200,align: 'center' }
        ,{field: 'iosCount', title: 'IOS注册人数' ,align: 'center' }
        ,{field: 'androidCount', title: '安卓注册人数',align: 'center' }
        ,{field: 'pcCount', title: '网页注册人数',align: 'center' }
    ]]

};

/**
 * 初始化第二个表格列
 */
VpnBusinessDataInfo.initColumn2 = function(){
    return [[
        {field: 'createTime', title: '日期', fixed: 'left',align: 'center' }
        ,{field: 'manyDeviceOnlineCount', title: '多设备上线人数',width:200,align: 'center' }
        ,{field: 'totalOnlineUserCount', title: '总上线用户人数',width:200,align: 'center' }
        ,{field: 'onlineTotalCount', title: '总上线次数' ,align: 'center' }
        ,{field: 'accountAverageOnlineTime', title: '账号平均上线时长(分钟)',
            width:200,align: 'center' }
        ,{field: 'deviceAverageOnlineTime', title: '设备平均上线时长(分钟)',width:200,align: 'center' }
    ]]
};

/**
 * 初始化第三个表格列
 */
VpnBusinessDataInfo.initColumn3 = function(){
    return [[
        {field: 'createTime', title: '日期', fixed: 'left',align: 'center' }
        ,{field: 'todayMemberCount', title: '当天购买人数',align: 'center' }
        ,{field: 'todayTotalAmount', title: '当天购买总金额（HKD）',align: 'center',width: 200 }
        ,{field: 'todayNonPaymentCount', title: '当天未购买人数',align: 'center' ,width: 200 }
        ,{field: 'todayBuyP0Count', title: '当天购买7天套餐人数',align: 'center' ,width: 260 }
        ,{field: 'todayBuyP1Count', title: '当天购买30天套餐人数',align: 'center' ,width: 260 }
        ,{field: 'todayBuyP2Count', title: '当天购买90天套餐人数',align: 'center' ,width:260}
        ,{field: 'todayBuyP3Count', title: '当天购买180天套餐人数' ,align: 'center' ,width: 260 }
        ,{field: 'todayBuyP4Count', title: '当天购买360天套餐人数',align: 'center' ,width: 260 }
        ,{field: 'buyP0TotalCount', title: '7天套餐总用户数',align: 'center',width: 260  }
        ,{field: 'buyP1TotalCount', title: '30天套餐总用户数',align: 'center',width: 260  }
        ,{field: 'buyP2TotalCount', title: '90天套餐总用户数',align: 'center' ,width: 260 }
        ,{field: 'buyP3TotalCount', title: '180天套餐总用户数',align: 'center' ,width: 260 }
        ,{field: 'buyP4TotalCount', title: '360天套餐总用户数',align: 'center' ,width: 260 }
        ,{field: 'totalNonPaymentCount', title: '未购买总人数',align: 'center' ,width: 200 }
        ,{field: 'totalPaymentRatio', title: '总用户付费率',
            align: 'center',templet:function (item) {
                var value = item.totalPaymentRatio * 100;
                if(value > 0){
                    value =  value.toFixed(2);
                }
                return value +"%";
            } }

    ]]
};


/**
 * 查询业务数据列表
 */
VpnBusinessDataInfo.search = function () {
    var queryData = {};
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();

    var target = $("#target").val();
    if(target == '1'){
        //查询拉新数据
        VpnBusinessDataInfo.table1.reload("table1Reload",{
            url: Feng.ctxPath +"/vpnBusinessDataInfo/newUserInfo",
            where: queryData
        });
    }
    else if(target == "2"){
        //查询用户留存数据
        VpnBusinessDataInfo.table2.reload("table2Reload",{
            url: Feng.ctxPath +"/vpnBusinessDataInfo/retentionUserInfo",
            where: queryData
        });
    }
    else if(target == "3"){
        //查询财务数据
        VpnBusinessDataInfo.table3.reload("table3Reload",{
            url: Feng.ctxPath +"/vpnBusinessDataInfo/financialInfo",
            where: queryData
        });
    }
    else{
        layer.msg("请选择要查询的指标类型");
    }
};

/**
 * 导出数据
 */
VpnBusinessDataInfo.exportExcel = function () {
    var beginTime = $("#beginTime").val();
    var endTime = $("#endTime").val();

    var target = $("#target").val();
    if(target == '-1'){
        //拉新数据
        layer.msg("请选择要查询的指标类型");
    }else{
        window.location.href =
            Feng.ctxPath + "/vpnBusinessDataInfo/download/excel?"
            +"type="+target+"&beginTime="+beginTime+"&endTime="+endTime;
    }

};

VpnBusinessDataInfo.table1List = function(queryData){
    layui.use("table",function () {
        var table = layui.table;

        VpnBusinessDataInfo.table1 = table;

        table.render({
            elem: "#table1",
            url: Feng.ctxPath +"/vpnBusinessDataInfo/newUserInfo",
            cellMinWidth: 120,
            title: "拉新用户核心指标",
            where: queryData,
            page: false,
            cols: VpnBusinessDataInfo.initColumn1(),
            parseData: function(res){
                return{
                    "code":0,
                    "msg":"获取成功",
                    "count":res.length,
                    "data": res
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "table1Reload"
        });
    });
};

VpnBusinessDataInfo.table2List = function(){
    layui.use("table",function () {
        var table = layui.table;

        VpnBusinessDataInfo.table2 = table;

        table.render({
            elem: "#table2",
            url: Feng.ctxPath +"/vpnBusinessDataInfo/retentionUserInfo",
            cellMinWidth: 120,
            title: "用户留存核心指标",
            page: false,
            cols:VpnBusinessDataInfo.initColumn2(),
            parseData: function(res){
                return{
                    "code":0,
                    "msg":"获取成功",
                    "count":res.length,
                    "data": res
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "table2Reload"
        });
    })
};

VpnBusinessDataInfo.table3List = function(){
    layui.use("table",function () {
        var table = layui.table;

        VpnBusinessDataInfo.table3 = table;

        table.render({
            elem: "#table3",
            url: Feng.ctxPath +"/vpnBusinessDataInfo/financialInfo",
            cellMinWidth: 120,
            title: "财务数据",
            page: false,
            cols:VpnBusinessDataInfo.initColumn3(),
            parseData: function(res){
                return{
                    "code":0,
                    "msg":"获取成功",
                    "count":res.length,
                    "data": res
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "table3Reload"
        });
    })
};

$(function () {
    VpnBusinessDataInfo.table1List();

    VpnBusinessDataInfo.table2List();

    VpnBusinessDataInfo.table3List();
});
